const util = require('util');
const debug = util.debuglog('oil-game:luckDraw:cache');

const {
  getLuckDrawInfo,    // 获取幸运抽奖
  addLuckDrawNum,   // 增加幸运抽奖次数
  saveLuckDraw,   // 保存幸运抽奖
  endLuckDraw,    //抽奖结束，更新奖品
} = require('./dao');
const {
  Msg,
  RESET_LUCK_DRAW_NUM,    //重置次数
  JACKPOT,    //奖池
  PRIZE_RESULT,   //奖品前缀
} = require('./consts');

// 获取幸运抽奖
exports.getLuckDrawInfo = async function(uid) {
  debug("用户 %s 获取幸运抽奖", uid);
  //用户离线
  let user = await this.app.userRemote(uid, 'get', [uid, 'user']);
  if(!user){ 
    debug("用户 %s 获取User升级信息，内存中没有此用户，返回null", uid);
    return null;
  }
  //如果内存中有此数据，则直接返回
  if(user.luckDraw){ return user.luckDraw; }
  //内存中不存在该表单数据，从数据库中查询
  luckDraw = await getLuckDrawInfo(uid) || {uid, luckDrawNum: RESET_LUCK_DRAW_NUM};
  debug("用户 %s 获取幸运抽奖，从数据库中获取挂载后返回 %s", uid, JSON.stringify(luckDraw));
  //更新内存中数据
  await this.app.userRemote(uid, "set", [uid, "user.luckDraw", luckDraw]);
  //返回从数据库查询的数据
  return luckDraw;
}

//增加抽奖次数
exports.addLuckDrawNum = async function(uid, num) {
  debug("uid = %s num = %s 增加抽奖次数", uid, num);
  //初始化内存中幸运抽奖
  let luckDraw = await exports.getLuckDrawInfo.call(this, uid);
  num += luckDraw.luckDrawNum;
  //增加抽奖次数
  await this.app.userRemote(uid, "set", [uid, "user.luckDraw.luckDrawNum", num]);
}

//减少抽奖次数
exports.reduceLuckDrawNum = async function(uid) {
  debug("uid = %s 减少抽奖次数", uid);
  //初始化内存中幸运抽奖
  await exports.getLuckDrawInfo.call(this, uid);
  //增加抽奖次数
  await this.app.userRemote(uid, "com", [uid, "user.luckDraw.luckDrawNum -= 1"]);
}

//同步幸运抽奖信息到服务器
// exports.syncLuckDrawData = async function(luckDraw) {
//   debug("同步幸运抽奖次数到数据库  %s", JSON.stringify(luckDraw));
//   await saveLuckDraw(luckDraw);
// }

//暂存用户抽奖结果
exports.savePrize = async function(uid, prize) {
  debug("uid = %s prize = %s 暂存用户抽奖结果", uid, prize);
  let redis = this.app.get('redis');
  let key = PRIZE_RESULT+":"+uid;

  return new Promise((resolve, reject) => {
    redis.del(key, function(err, data){   //重置抽奖结果
      if(err){
        debug("uid = %s 清理用户抽奖结果异常 %s", uid, JSON.stringify(err));
        return reject(err);
      }
      redis.hmset(key, prize, function(err, data){   //设置倒计时键
        if(err){
          debug("uid = %s 暂存用户抽奖结果异常 %s", uid, JSON.stringify(err));
          return reject(err);
        }
        debug("uid = %s 暂存用户抽奖结果成功 %s", uid, JSON.stringify(data));
        resolve(data);
      });
    });
  });
}

//获取用户抽奖结果
exports.getPrize = async function(uid) {
  debug("uid = %s 获取用户抽奖结果", uid);
  let redis = this.app.get('redis');
  let key = PRIZE_RESULT+":"+uid;

  return new Promise((resolve, reject) => {
    redis.hgetall(key, function(err, data){   //设置倒计时键
      if(err){
        debug("uid = %s 获取用户抽奖结果异常 %s", uid, JSON.stringify(err));
        return reject(err);
      }
      redis.del(key, function(err, data1){   //获取用户抽奖结果
        if(err){
          debug("uid = %s 获取用户抽奖结果异常 %s", uid, JSON.stringify(err));
          return reject(err);
        }
        debug("uid = %s 获取用户抽奖结果成功 %s", uid, JSON.stringify(data));
        resolve(data);
      });
    });
  });
}
